/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import pojo.Hoadondeal;
import util.HibernateUtil;

/**
 *
 * @author Katori
 */
public class HoaDonDealDAO {
        public static List<Hoadondeal> layTatCaHoaDonDeal() {
        List<Hoadondeal> dsHoaDon = null;
        SessionFactory ssFac = HibernateUtil.getSessionFactory();
        Session ss = ssFac.getCurrentSession();
        Transaction trans = ss.getTransaction();
        trans.begin();
        try {
            String hql = "from pojo.Hoadondeal";
            dsHoaDon = ss.createQuery(hql).list();
            trans.commit();
        } catch (HibernateException ex) {
            System.out.println(ex.getMessage());
        }
        return dsHoaDon;
    }

    public static Hoadondeal layHoaDonDeal(int maHoaDon) {
        Hoadondeal hoaDon = null;
        SessionFactory ssFac = HibernateUtil.getSessionFactory();
        Session ss = ssFac.getCurrentSession();
        Transaction trans = ss.getTransaction();
        trans.begin();
        try {
            hoaDon = (Hoadondeal) ss.get(Hoadondeal.class, maHoaDon);
            trans.commit();
        } catch (HibernateException ex) {
            System.out.println(ex.getMessage());
        }
        return hoaDon;
    }

    public static boolean themHoaDonDeal(Hoadondeal hoaDon) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            session.beginTransaction();
            session.save(hoaDon);
            session.getTransaction().commit();
            return true;
        } catch (Exception e) {
            session.getTransaction().rollback();
            System.out.println(e);
            return false;
        } finally {
            session.close();
        }
    }

    public static boolean capNhatHoaDonDeal(Hoadondeal loai) {
        if (HoaDonDealDAO.layHoaDonDeal(loai.getMaHoaDonDeal()) == null) {
            return false;
        }

        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        try {
            session.getTransaction().begin();
            session.update(loai);
            session.getTransaction().commit();
            return true;
        } catch (Exception e) {
            session.getTransaction().rollback();
            System.out.print(e);
            return false;
        }
    }

    public static boolean xoaHoaDonDeal(int maHoadondeal) {
        if (HoaDonDealDAO.layHoaDonDeal(maHoadondeal) == null) {
            return false;
        }

        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        try {
            session.getTransaction().begin();
            String hql = "update pojo.Hoadondeal set BiXoa = :xoa where MaHoadondeal = :ma";
            Query query = session.createQuery(hql);
            query.setString("xoa", "1");
            query.setString("ma", Integer.toString(maHoadondeal));
            int rowCount = query.executeUpdate();
            session.getTransaction().commit();
            
            if (rowCount > 0) {
                return true;
            } else {
                return false;
            }

        } catch (Exception e) {
            session.getTransaction().rollback();
            System.out.print(e);
            return false;
        }
    }
}
